package com.romotc.qqrobot.webqq.qqInterfaceCall;

import java.util.ArrayList;

import org.json.JSONObject;

import com.romotc.common.http.CookieUtil;
import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.SysConstant;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.DateProcessor;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.Ptlogin2_Login_Processor;
import com.romotc.qqrobot.webqq.util.QQRandomUtil;
import com.romotc.scache.RequestSpy;
import com.romotc.scache.httpreq;
/**
 * 二次登录接口调用
 * @author romotc
 *
 */
public class D_Login2_InterCall extends QQInterfaceCall {

	private static D_Login2_InterCall instance;
	public static synchronized D_Login2_InterCall getInstance() {
		if(instance == null)
			instance = new D_Login2_InterCall();
		
		return instance;
	}
	
	/*
	 * 登陆参数：登陆状态 隐身 正常等
	 */
	public static final String Key_Login_Param_Status = "Login_Param_Status";
	
	public static final String Key_Login_Param_QQ = "Login_Param_QQ";
	public static final String Key_Login_Param_Pswd = "Login_Param_Pswd";

	@Override
	public JSONObject preCall(QQLogedin loginQQ, JSONObject reqDataObj) 
			throws Exception {
		Ptlogin2_Login_InterCall call = Ptlogin2_Login_InterCall.getInstance();
		reqDataObj = call.call(loginQQ, reqDataObj, Ptlogin2_Login_Processor.getInstance());
		
		return reqDataObj;
	}

	@Override
	public boolean hasPreCall(QQLogedin loginQQ, JSONObject reqDataObj) {
		return true;
	}

	@Override
	public ArrayList<String> myReqHeaders(int reqBodyLen, QQLogedin loginQQ,JSONObject reqDataObj) {
//		Accept:*/*
//		Accept-Charset:UTF-8,*;q=0.5
//		Accept-Encoding:gzip,deflate,sdch
//		Accept-Language:zh-CN,zh;q=0.8
//		Connection:keep-alive
//		Content-Length:246
//		Content-Type:application/x-www-form-urlencoded
//		Cookie:pgv_pvid=4802251185; pgv_info=pgvReferrer=&ssid=s8656846571; ptui_loginuin=97190865; pt2gguin=o0097190865; uin=o0097190865; skey=@6ljcO4utT; ptisp=cnc; show_id=; ptwebqq=a4d99e91a9f51fd023cf771ea122d831cc0d20ff8b5de277ddfc4cf85e97b20f
//		Host:d.web2.qq.com
//		Origin:http://d.web2.qq.com
//		Referer:http://d.web2.qq.com/proxy.html?v=20110331002&callback=2
//		User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.83 Safari/535.11
		ArrayList<String> header = new ArrayList<String>();
		header.add("Accept:*/*");
		header.add("Accept-Charset:UTF-8,*;q=0.5");
		header.add("Accept-Encoding:gzip,deflate,sdch");
		header.add("Accept-Language:zh-CN,zh;q=0.8");
		header.add("Connection:keep-alive");
		header.add("Content-Length:" + reqBodyLen);
		header.add("Content-Type:application/x-www-form-urlencoded");
		header.add("Cookie:" + loginQQ.getCookieString());
		header.add("Host:d.web2.qq.com");
		header.add("Origin:d.web2.qq.com");
		header.add("Referer:http://d.web2.qq.com/proxy.html?v=20110331002&callback=2");
		header.add("User-Agent:" + SysConstant.User_Agent);
		return header;
	}

	@Override
	public String buildReqBody(JSONObject reqDataObj, QQLogedin loginQQ)
			throws Exception {
		//ptwebqq
		JSONObject cookie = CookieUtil.parseCookieJsonFromStr(loginQQ.getCookieString());
		String ptwebqq = cookie.getString(WebQQConstant.Cookie_ptwebqq);
		
		//status
		String status = reqDataObj.optString(Key_Login_Param_Status, WebQQConstant.Login_Param_Status_Default);//默认隐身登陆
		String clientId = QQRandomUtil.getRandomClientId(WebQQConstant.Webqq_ClientId_Length);
		
		//r:{"status":"away","ptwebqq":"765faf68f855f6a13902f0201ce14341259ca4f17049b1003d43df5debb13d3d","passwd_sig":"","clientid":"92750457","psessionid":null}
		//clientid:92750457
		//psessionid:null
		StringBuilder bld = new StringBuilder();
		bld.append("r={\"status\":\"").append(status).append("\",");
			bld.append("\"ptwebqq\":\"").append(ptwebqq).append("\",");
			bld.append("\"passwd_sig\":\"\",");
			bld.append("\"clientid\":\"").append(clientId).append("\",");
			bld.append("\"psessionid\":null");
			bld.append("}");
			
		bld.append("&clientid=").append(clientId);
		bld.append("&psessionid=null");
		return bld.toString();
	}

	@Override
	public int getLogLevel() {
		return 1001;
	}

	@Override
	public int getReq_method() {
		return httpreq.REQUEST_POST;
	}

	@Override
	public String getReqUrl(JSONObject reqDataObj, QQLogedin loginQQ) throws Exception {
		return WebQQConstant.URL_D_LOGIN;
	}

	
	public static void main(String[] args) throws Exception {
		D_Login2_InterCall call = D_Login2_InterCall.getInstance();
		QQLogedin curQqLogedIn = new QQLogedin();
		JSONObject reqDataObj = new JSONObject();
		reqDataObj.put(Key_Login_Param_QQ, "123");
		reqDataObj.put(Ptlogin2_Login_InterCall.Key_Login_Param_Pswd, "xxxx");
		
		call.call(curQqLogedIn, reqDataObj, new DateProcessor() {
			
			@Override
			public JSONObject processResponse(QQLogedin loginQQ, RequestSpy rqSpy,
					JSONObject reqDataObj) throws Exception {
				//ptuiCB('0','0','http://web.qq.com/loginproxy.html?login2qq=1&webqq_type=30','0','登录成功！', 'Romotc');
				//ptuiCB('3','0','','0','您输入的帐号或者密码不正确，请重新输入。', '97190867');
				String resbody = rqSpy.getResBody();
				System.out.println(resbody);
				loginQQ = QQLogedin.buildQQLogedIn(rqSpy);
				
				loginQQ.startKeepOnlineThread();
				return reqDataObj;
			}
		});
	}

}
